<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item prop="organization" label="所属组织:">
              <el-select
                filterable
                clearable
                v-model="where.organization"
                @change="getDepartmentsList"
                size="small"
                placeholder="-请选择所属组织-"
                class="ele-block">
                <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item prop="superior" label="所属院系:">
              <el-select
                filterable
                clearable
                v-model="where.superior"
                @change="getClassList"
                size="small"
                placeholder="-请选择院系-"
                class="ele-block">
                <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item ref="class" prop="class" label="所属班级:">
              <el-select
                filterable
                clearable
                v-model="where.class"
                @change="$forceUpdate()"
                size="small"
                placeholder="-请选择班级-"
                class="ele-block">
                <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="任务类型:">
              <el-select
                clearable
                v-model="where.type"
                placeholder="请选择任务类型"
                class="ele-fluid">
                              <el-option label="考试任务" value="1"/>
                              <el-option label="日常练习" value="2"/>
                            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :lg="7" :md="24">
            <el-form-item label="任务时间:">
              <el-col :lg="10" :md="20">
                <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.starttime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择开始日期"/>
              </el-col>
              <el-col :lg="2" :md="12">
                至
              </el-col>
              <el-col :lg="10" :md="20">
                <el-date-picker
                type="date"
                class="ele-fluid"
                v-model="where.endtime"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="请选择结束日期"/>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="12">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="全部" value=""/>
                <el-option label="正常" value="1"/>
                <el-option label="停用" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="12">
            <el-form-item ref="create_user" prop="create_user" label="发布老师:">
              <el-select
                filterable
                clearable
                v-model="where.create_user"
                @change="$forceUpdate()"
                size="small"
                placeholder="-请选择发布老师-"
                class="ele-block">
                <el-option v-for="item in teacherList" :key="item.id" :label="item.realname" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="12">
            <el-form-item ref="standard" prop="standard" label="考核标准:">
              <el-select
                filterable
                clearable
                v-model="where.standard"
                @change="$forceUpdate()"
                size="small"
                placeholder="-请选择考核标准-"
                class="ele-block">
                <el-option v-for="item in standardList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :lg="6" :md="12">
              <el-form-item label="任务编号:">
                <el-input
                  clearable
                  v-model="where.code"
                  placeholder="请输入任务编号"/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="使用设备:">
                <el-input
                  clearable
                  v-model="where.equipment"
                  placeholder="请输入使用设备"/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <div class="ele-form-actions">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  @click="reload">查询
                </el-button>
                <el-button @click="reset">重置</el-button>
              </div>
            </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:task:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:task:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary"
                   :underline="false"
                   icon="el-icon-view"
                   @click="openDetail(row)">成绩
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:task:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此任务吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:task:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>





          <!-- 任务状态列 -->
          <template slot="status" slot-scope="{row}">
            <el-switch
              v-model="row.status"
              @change="status(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>


          <!-- 任务类型列 -->
          <template slot="model" slot-scope="{row}">
                      <el-tag v-if="row.type === 0" type="" size="small">测试模式</el-tag>
                      <el-tag v-if="row.type === 1" type="success" size="small">训练模式</el-tag>
                    </template>






                        </ele-pro-table>
    </el-card>
     <!-- 详情弹窗 -->
    <detail-modal
    :data="current"
    :visible.sync="showDetailModal"/>
    <!-- 编辑弹窗 -->
    <task-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import DetailModal from '../taskscorerecord/detail';
import TaskEdit from './task-edit';

export default {
  name: 'ExamTask',
  components: {DetailModal,TaskEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/task/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        //       {
        //   prop: 'code',
        //   label: '编号',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        // },
                    {
          prop: 'name',
          label: '任务名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'organization_name',
          label: '所属组织',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'superior_name',
          label: '所属院系',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                          {
          prop: 'status',
          label: '任务状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
                          {
          prop: 'class_name',
          label: '所属班级',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                          {
          prop: 'model',
          label: '任务模式',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'model',
        },
                          {
          prop: 'task_start',
          label: '任务开始时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'task_end',
          label: '任务结束时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'munumber',
          label: '目标人数',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'actualnumber',
          label: '实际参与人数',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'sort',
          label: '显示顺序',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                    {
          prop: 'note',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
              {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 200,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示任务成绩详情
      showDetailModal:false,
      // 是否显示编辑弹窗
      showEdit: false,
      // 学院列表
      schoolList: [],
      // 院系列表
      departmentsList: [],
      // 班级列表
      classList: [],
      // 老师列表
      teacherList: [],
      // 考核标准列表
      standardList: [],
    };
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    // 获取院系列表
    this.getDepartmentsList();
    // 获取班级列表
    this.getClassList();
    // 获取老师列表
    this.getTeacherList();
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 查看详情 */
    openDetail(row) {
      this.current = row;
      this.showDetailModal = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/task/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的任务吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/task/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },

    /* 更改任务状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/task/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取院系列表
     */
    getDepartmentsList() {
      if(this.where.organization){
        this.where.superior = null;
        this.where.class = null;
        this.where.standard = null;
        this.getStandardList();
        this.$forceUpdate();
      }
      this.$http.post('/departments/getDepartmentsList', {sid: this.where.organization}).then(res => {
        if (res.data.code === 0) {
          this.departmentsList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取班级列表
     */
    getClassList() {
      if(this.where.superior){
        this.where.class = null;
        this.$forceUpdate();
      }
      this.$http.post('/clss/getClassList',{yid:this.where.superior}).then(res => {
        if (res.data.code === 0) {
          this.classList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取老师列表
     */
    getTeacherList() {
      this.$http.post('/user/getTeacherList',{yid:this.where.superior}).then(res => {
        if (res.data.code === 0) {
          this.teacherList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取考核标准列表
     */
    getStandardList() {
      this.$http.post('/standard/getStandardList',{oid:this.where.organization}).then(res => {
        if (res.data.code === 0) {
          this.standardList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
                                                        }
}
</script>

<style scoped>
</style>
